package com.oglvip.business.web.controller;

import com.oglvip.business.dto.ResponseData;
import com.oglvip.business.remote.service.api.GoodsService;
import com.oglvip.business.remote.service.api.StoreService;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @auther hanchao
 * @date 2016/12/11 17:37
 */
@Controller
@RequestMapping("/upload")
public class UploadController {
    @Autowired
    private StoreService storeService;
    @Autowired
    private GoodsService goodsService;

    @RequestMapping("/img")
    @ResponseBody
    public ResponseData uploadImg(MultipartFile file,String type) throws IOException {
        String imgData = Base64.encodeBase64String(file.getBytes());
        return storeService.uploadImg(imgData,type);
        //return ResponseData.builder().data("asdasdasd").build();
    }

    @RequestMapping("/goods/img")
    @ResponseBody
    public ResponseData uploadGoodsImg(MultipartFile file,String type) throws IOException {
        String imgData = Base64.encodeBase64String(file.getBytes());
        return goodsService.uploadImg(imgData,type);
    }
    /*@RequestMapping("/fileShow/{path}")
    @ResponseBody
    public void test(@PathVariable String path, HttpServletRequest request, HttpServletResponse response) {
        long header = request.getDateHeader("If-Modified-Since");
        if (header > 0) {
            response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
            return;
        }
        long current = System.currentTimeMillis();
        long expires = current + 86400000;
        response.addDateHeader("Expires", expires);
        response.addDateHeader("Last-Modified", current);

        try {
            if (StringUtils.isNotBlank(path)) {
                FileUtils.copyFile(new File(PATH_ROOT + File.separator + path), response.getOutputStream());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }*/
}
